package com.jlastu.znb.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.znb.R;
import com.jlastu.znb.database.DBHelper;
import com.jlastu.znb.message.UserSession;
import com.jlastu.znb.utils.SpUtils;

public class LoginActivity extends AppCompatActivity {

    private EditText loginUsername;
    private EditText loginPassword;
    private DBHelper dbHelper;
    private Button btn1;
    private SpUtils mSpUtils;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        loginUsername = findViewById(R.id.login_username);
        loginPassword = findViewById(R.id.login_password);
        dbHelper = new DBHelper(this); // 初始化数据库帮助类
        mSpUtils = new SpUtils(this);
        Button loginButton = findViewById(R.id.login_button);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 登录逻辑
                String username = loginUsername.getText().toString();
                String password = loginPassword.getText().toString();
                if (login(username, password)) {
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    UserSession.getInstance().setUsername(username);
                    startActivity(intent);
                } else {
                    Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
        btn1=findViewById(R.id.zc);

        // 监听点击事件
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到另一个名为ButtonActivity的界面
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    private boolean login(String username, String password) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        boolean success = DBHelper.loginUser(db, username, password);
        if (success) {
            int userId = getCurrentUserIdFromDb(db, username);
            if (userId != -1) {
                mSpUtils.putInt(SpUtils.LOGIN_KEY, userId); // 保存用户ID到SharedPreferences
                Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
            }
        }
        db.close();
        return success;
    }
    @SuppressLint("Range")
    private int getCurrentUserIdFromDb(SQLiteDatabase db, String username) {
        String[] columns = { "_id" };
        String selection = "user_name = ?";
        String[] selectionArgs = { username };
        Cursor cursor = db.query("user", columns, selection, selectionArgs, null, null, null);
        int userId = -1;
        if (cursor.moveToFirst()) {
            userId = cursor.getInt(cursor.getColumnIndex("_id"));
        }
        cursor.close();
        return userId;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dbHelper != null) {
            dbHelper.close(); // 关闭数据库
        }
    }
}
